<%= form_for @check, html: {class: "small-form"} do |f| %>
  <% unless @check.respond_to?(:check_type) || @check.respond_to?(:invert) %>
    <p class="text-muted">Checks are designed to identify bad data. A check fails if there are any results.</p>
  <% end %>

  <% if @check.errors.any? %>
    <div class="alert alert-danger"><%= @check.errors.full_messages.first %></div>
  <% end %>

  <div class="form-group">
    <%= f.label :query_id, "Query" %>
    <div class="hide">
      <%= f.select :query_id, [], {include_blank: true} %>
    </div>
    <%= javascript_tag nonce: true do %>
      <%= blazer_js_var "queries", Blazer::Query.active.named.order(:name).select("id, name").map { |q| {text: q.name, value: q.id} } %>
      <%= blazer_js_var "items", [@check.query_id].compact %>

      $("#check_query_id").selectize({options: queries, items: items, highlight: false, maxOptions: 100}).parents(".hide").removeClass("hide");
    <% end %>
  </div>

  <% if @check.respond_to?(:check_type) %>
    <div class="form-group">
      <%= f.label :check_type, "Alert if" %>
      <div class="hide">
        <% check_options = [["Any results (bad data)", "bad_data"], ["No results (missing data)", "missing_data"]] %>
        <% check_options << ["Anomaly (most recent data point)", "anomaly"] if Blazer.anomaly_checks %>
        <%= f.select :check_type, check_options %>
      </div>
      <%= javascript_tag nonce: true do %>
        $("#check_check_type").selectize({}).parent().removeClass("hide");
      <% end %>
    </div>
  <% elsif @check.respond_to?(:invert) %>
    <div class="form-group">
      <%= f.label :invert, "Fails if" %>
      <div class="hide">
        <%= f.select :invert, [["Any results (bad data)", false], ["No results (missing data)", true]] %>
      </div>
      <%= javascript_tag nonce: true do %>
        $("#check_invert").selectize({}).parent().removeClass("hide");
      <% end %>
    </div>
  <% end %>

  <% if @check.respond_to?(:schedule) && Blazer.check_schedules %>
    <div class="form-group">
      <%= f.label :schedule, "Run every" %>
      <div class="hide">
        <%= f.select :schedule, Blazer.check_schedules.map { |v| [v, v] } %>
      </div>
      <%= javascript_tag nonce: true do %>
        $("#check_schedule").selectize({}).parent().removeClass("hide");
      <% end %>
    </div>
  <% end %>

  <div class="form-group">
    <%= f.label :emails %>
    <%= f.text_field :emails, placeholder: "Optional, comma separated", class: "form-control" %>
  </div>

  <% if Blazer.slack? %>
    <div class="form-group">
      <%= f.label :slack_channels %>
      <%= f.text_field :slack_channels, placeholder: "Optional, comma separated", class: "form-control" %>
    </div>
  <% end %>

  <p class="text-muted">Emails <%= Blazer.slack? ? "and Slack notifications " : nil %>are sent when a check starts failing, and when it starts passing again.
  <p>
    <% if @check.persisted? %>
      <%= link_to "Delete", check_path(@check), method: :delete, "data-confirm" => "Are you sure?", class: "btn btn-danger" %>
    <% end %>
    <%= f.submit "Save", class: "btn btn-success" %>
    <%= link_to "Back", :back, class: "btn btn-link" %>
  </p>
<% end %>
